﻿package edu.pku.se.chocolate.UI.Adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.datastructure.Category;

public class MainAdapter extends BaseAdapter {
	// 定义Context
	private Context		mContext;
	// 定义整型数组 即图片源
	private Integer[]	mImageIds	= 
	{ 
			R.drawable.category1, 
			R.drawable.category2, 
			R.drawable.category3, 
			R.drawable.category4, 
			R.drawable.category5,
			R.drawable.category6,
			R.drawable.category7,
			R.drawable.category8,
			R.drawable.category9,
			R.drawable.category10,
			R.drawable.keyword,
			//R.drawable.category5,
	};
	private String[] mInfo = 
	{
			"二手市场",
			"BBS-十大",
			"讲座",
			"演出",
			"公告",
			"征恋人",
			"征玩友",
			"社团",
			"国际交流",
			"寻物&招领",
			"自定义类",
	};
	
	final private ArrayList<Category> categoryList;
	
	public MainAdapter(Context c)
	{
		categoryList = ChocolateActivity.c_solver.getCategoryList();
		mContext = c;
		Iterator<Category> ci = categoryList.iterator();
		for(int i=0;i<11;i++) {
			Category temp = ci.next();
			visible[i] = temp.isShown;
			mInfo[i] = temp.getDisplayName();
		}
//		visible[5] = false;
		for(int i=0;i<size;i++) {
			if(visible[i])
				visiblePosition.add(i);
		}
	}

	// 获取图片的个数
	public int getCount()
	{
		return visiblePosition.size();
	}

	
	public Object getItem(int position)
	{
		return position;
	}


	
	public long getItemId(int position)
	{
		return position;
	}


	public View getView(int position, View convertView, ViewGroup parent)
	{
		////////////////////////
		LinearLayout ll;
		ImageView iv;
		TextView tv;
		if (convertView == null){
			ll = new LinearLayout(mContext);
			ll.setOrientation(LinearLayout.VERTICAL);
			
			iv = new ImageView(mContext);
			iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
			ll.addView(iv);
			
			tv = new TextView(mContext);
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(android.graphics.Color.BLACK);
//			tv.getPaint().setFakeBoldText(true);
			ll.addView(tv);
		} else{
			ll = (LinearLayout) convertView;
			iv = (ImageView) ll.getChildAt(0);
			tv = (TextView) ll.getChildAt(1);
		}
		//3转换为categoryList.get(visiblePosition.get(position)).newcount
		iv.setImageBitmap(generatorContactCountIcon(getResIcon(mContext.getResources(),mImageIds[visiblePosition.get(position)]),categoryList.get(visiblePosition.get(position)).getNewMsgNum()));
		tv.setText(mInfo[visiblePosition.get(position)]);
		tv.setTextColor(0xff000000);
		return ll;
		
	}
	
	private final int size = 11;
	private boolean[] visible = new boolean[size];
	//可显示的图片编号
	private LinkedList<Integer> visiblePosition = new LinkedList<Integer>();
	
	//对象i可见
	public void choose(int i) {
		visible[i] = true;
		int j;
		Iterator<Category> ci = categoryList.iterator();
		for(j=0;j<i;j++)
			ci.next();
		ChocolateActivity.c_solver.setIsCategoryShow(ci.next(), true);
		for(j=0;j<visiblePosition.size();j++) {
			if(visiblePosition.get(j)>i)
				break;
		}
		if(j>0)
			if(visiblePosition.get(j-1)==i)
				return;
		visiblePosition.add(j, i);
	}
	
	//对象i不可见
	public void unchoose(int i) {
		visible[i] = false;
		int j;
		Iterator<Category> ci = categoryList.iterator();
		for(j=0;j<i;j++)
			ci.next();
		ChocolateActivity.c_solver.setIsCategoryShow(ci.next(), false);
		for(j=0;j<visiblePosition.size();j++) {
			if(visiblePosition.get(j)==i) {
				visiblePosition.remove(j);
				break;
			}
		}
	}
	
	public LinkedList<Integer> getLinkedList() {
		return visiblePosition;
	}
	
	public boolean isVisible(int i) {
		return visible[i];
	}
	
	/**
	 * 根据id获取一个图片
	 * @param res
	 * @param resId
	 * @return
	 */ 
	private Bitmap getResIcon(Resources res,int resId) { 
		return BitmapFactory.decodeResource(res, resId);
	}

	/**
     * 在给定的图片的右上角加上数量。数量用红色表示
     * @param icon 给定的图片
     * @return 带数量的图片
     */ 
	private Bitmap generatorContactCountIcon(Bitmap icon,int newcount){ 
//		newcount = 12;
        //初始化画布 
		float tempSize = mContext.getResources().getDimension(android.R.dimen.app_icon_size); 
        int iconSize=(int)mContext.getResources().getDimension(android.R.dimen.app_icon_size); 
        Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888); 
        Canvas canvas=new Canvas(contactIcon);      
        //拷贝图片 
        Paint iconPaint=new Paint(); 
        iconPaint.setDither(true);//防抖动 
        iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理，这样，当你选择Drawable时，会有抗锯齿的效果 
        Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight()); 
        Rect dst=new Rect(0, 0, iconSize, iconSize); 
        canvas.drawBitmap(icon, src, dst, iconPaint); 
        if(newcount>99)
        	newcount = 99;
        //在图片上创建一个覆盖的个数 
        int contacyCount=newcount; 
        //启用抗锯齿和使用设备的文本字距 
        Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG); 
        countPaint.setColor(Color.RED); 
        countPaint.setTextSize(tempSize*(20f/72f)); 
        countPaint.setTypeface(Typeface.DEFAULT_BOLD); 
        int count = 0;
        int base = 1;
        while(true){
        	int tempans = newcount/base;
        	if(tempans>0) {
        		count++;
        		base*=10;
        	}
        	else
        		break;
        }
        if(contacyCount>0)
        	canvas.drawText(String.valueOf(contacyCount), tempSize-14f*count*(tempSize/72f), (tempSize/72f)*14f, countPaint); 
        return contactIcon; 
    } 
}
